home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d17 / wrap.arc / WRAP.C < prev    next >
Text File  |  1986-03-06  |  7KB  |  300 lines

  1. /* copyright (c) 1982 Peter Baker */
  2.  
  3. /*
  4.    Wrap files to the standard output with any of a number of handy
  5.    options.  This program grew all out of proportion with people
  6.    continually asking for an extra feature.  Please don't curse me
  7.    for the poor code ... I was only trying to do them a favour.  Do
  8.    not try to add any more features without restructuring the whole
  9.    thing or it is liable to explode!!!
  10. */
  11. #include <stdio.h>
  12.  
  13. #define NEWLINE        '\n'
  14. #define CR            '\r'
  15. #define TAB            '\t'
  16. #define BKSPC        '\b'
  17. #define FORMFEED    '\f'
  18. #define SPACE        ' '
  19.  
  20.  
  21. #ifndef true
  22. #define true 1
  23. #define false 0
  24. #endif
  25.  
  26. /*
  27.  * See 'wrap.doc' for info and options.
  28.  */
  29.  
  30. int BIGBUF = 0x1000;            /* 4K ... could be up to ~ 40K */
  31. int width = 80;
  32. int length = 60;            /* standard 8 1/2 by 11 inch paper */
  33. int headings = false;
  34. int tabs = false;
  35. int tabsize;
  36. int slinenum = 0;            /* start printing at line N */
  37. int indent;
  38. int date = false;
  39. char *filename;
  40. FILE filep;
  41. char *datecode;
  42. int colcnt;
  43. int linecnt;
  44. int pagecnt;
  45.  
  46. main( argc, argv )
  47. int argc;
  48. char *argv[];
  49. {
  50.     static int fnum = 1;    /* arg # of the first filename if no opt */
  51.     static int optnum = 1;
  52.  
  53.     if( --argc < 1 )
  54.         usage();    /* this exits !! */
  55.  
  56.     while( *argv[optnum] == '-' )    /* options ?? */
  57.     {
  58.         if( --argc < 1 )    /* must be one arg left */
  59.             usage();    /* usage() exits! */
  60.         fnum++;    /* since this arg is an option */
  61.         switch( *(argv[optnum] +1) )
  62.         {
  63.             case 'w':
  64.             case 'W': width = atoi( argv[optnum] + 2 );
  65.                       /* skip '-w' */
  66.                       break;
  67.             case 'p':
  68.             case 'P': length = atoi( argv[optnum] + 2 );
  69.                       break;
  70.             case 'h':
  71.             case 'H': headings = true;
  72.                       length -= 3;    /* to acct for hdg space */
  73.                       break;
  74.             case 't':
  75.             case 'T': tabs = true;
  76.                       tabsize = atoi( argv[optnum] + 2 );
  77.                       if( tabsize == 0 )
  78.                           tabsize = 8;    /* default */
  79.                       break;
  80.             case 'd':
  81.             case 'D': date = true;
  82.                       headings = true;
  83.                       datecode = argv[optnum] + 2;
  84.                       break;
  85.             case 'l':
  86.             case 'L': slinenum = atoi( argv[optnum] + 2 );
  87.                       break;
  88.             case 'i':
  89.             case 'I': indent = atoi( argv[optnum] + 2 );
  90.                       break;
  91.             case 'b':
  92.             case 'B': BIGBUF = atoi( argv[optnum] + 2 );
  93.                       break;
  94.             default:  usage();
  95.         }
  96.         optnum++;    /* process next option */
  97.     }
  98. #ifdef DEBUG
  99. printf( "argv[1] = %s\n", argv[1] );
  100. printf( "\nwidth = %d\n", width );
  101. printf( "argv[2] = %s\n", argv[2] );
  102. printf( "length = %d\n", length );
  103. #endif
  104.     while( argc-- )
  105.     {
  106.         filename = argv[fnum];
  107.         filep = fopen( filename, "r" );
  108.         if( filep == 0 )
  109.             printf( "\nwrap cannot open: %s\n", filename );
  110.         else
  111.         {
  112.             wrap();
  113.             fclose( filep );
  114.             if( length > 0 )
  115.                 putchar( FORMFEED );
  116.             putchar( NEWLINE );
  117.         }
  118.         fnum++;
  119.     }
  120. }
  121.  
  122. wrap()
  123. {
  124.     static int c;        /* this is an int so it can hold an EOF */
  125.     static int spcs, i, lntemp;
  126.  
  127.     fillbuf();            /* this is needed here for some unknown reason */
  128.     linecnt = 0;
  129.     colcnt = indent;
  130.     pagecnt = 1;
  131.     if( headings )
  132.         prhdg( pagecnt++ );
  133.  
  134.     if( slinenum )        /* wait for the slinenum'th line before we print */
  135.         for( lntemp=0 ; lntemp < slinenum ; lntemp++ )
  136.             while( ( ( c = bgetc() ) != '\n' ) && ( c != EOF ) )
  137.                 ;
  138.     doindent();
  139.     while( ( c = bgetc() ) != EOF )
  140.     {
  141.         switch( c )
  142.         {
  143.             case NEWLINE:    colcnt = indent;
  144.                             linecnt++;
  145.                             break;
  146.             case CR:        colcnt = indent;
  147.                             break;
  148.             case TAB:        spcs = tabsize - ( (colcnt-indent) % tabsize );
  149.                             /* this starts tabs at the indent column not 0 */
  150.                             if( spcs == 0 )
  151.                                 spcs = tabsize;
  152.                             colcnt += spcs;
  153.                             if( tabs )
  154.                                 for( i=0 ; i<spcs ; i++ )
  155.                                     putchar( SPACE );
  156. #ifdef DEBUG
  157. printf( "\n spcs = %d\n", spcs );
  158. #endif
  159.                             break;
  160.             case BKSPC:        colcnt -= 1;
  161.                             break;
  162.             case FORMFEED:    linecnt = 0;
  163.                             colcnt = indent;
  164.                             break;
  165.             default :        colcnt++;
  166.         }
  167.         if( colcnt >= width )
  168.         {
  169.             putchar( NEWLINE );
  170.             doindent();
  171.             colcnt = indent;
  172.             linecnt++;
  173.         }
  174.  
  175.         if( tabs && ( c == TAB ) )
  176.             ;    /* don't print the tab if already done */
  177.         else
  178.             putchar( c );
  179.  
  180.         if( c == '\n' )
  181.             doindent();
  182.  
  183.         if( headings && ( c == FORMFEED ) )
  184.         {
  185.             prhdg( pagecnt++ );
  186.             doindent();
  187.         }
  188.         else if( c == FORMFEED )
  189.             doindent();
  190.  
  191.         if( length > 0 )
  192.             if( linecnt >= length )
  193.             {
  194.                 putchar( FORMFEED );
  195.                 if( headings )
  196.                     prhdg( pagecnt++ );
  197.                 putchar( CR );        /* cursor at col 0 */
  198.                 doindent();
  199.                 linecnt = 0;
  200.                 colcnt = indent;
  201.             }
  202.     }
  203. }
  204.  
  205.  
  206. prhdg( pagenum )
  207. int pagenum;
  208. {
  209.     putchar( NEWLINE );
  210.     doindent();
  211.     printf( "       File:    %s", filename );
  212.     printf( "       Page:  %d", pagenum );
  213.     if( date )
  214.         printf( "       Date:    %s", datecode );
  215.     putchar( NEWLINE );
  216.     putchar( NEWLINE );
  217. }
  218.  
  219.  
  220. usage()
  221. {
  222. /* The msg is broken up due to C/80 limiting line length to 100 chars. */
  223.  
  224. static char *msg = "[-h] [-ttabsize] [-dstring] fname [fname fname ...]";
  225.  
  226.     printf( "\nUsage: wrap [-wpagewidth] [-ppagelength] %s\n", msg );
  227.     printf( "where:\t-wN  sets the pagewidth to N\n" );
  228.     printf( "\t-pN  sets the pagelength to N\n" );
  229.     printf( "\t-h   turns on headings at the top of each page\n" );
  230.     printf( "\t-tN  expands tabs to every Nth column\n" );
  231.     printf( "\t-dstring sets the date to string and turns on headings\n" );
  232.     printf( "\t-lN  starts printing at the Nth line of the file\n" );
  233.     printf( "\t-iN  indents each line N spaces\n" );
  234.     printf( "\t-bN  sets the file buffer to N bytes\n" );
  235.     exit(1);
  236. }
  237.  
  238. static unsigned numchars = BIGBUF;
  239. /* Number of chars in buffer.  Set to BIGBUF to get bgetc() started. */
  240.  
  241. static int remchars = 0;    /* # of available chars remaining in buffer */
  242. static char *begbuf;        /* pointer to beginning of buffer */
  243. static char *curbyte;        /* pointer to next available byte in buffer */
  244. static char kickit = true;    /* initialize flag */
  245.  
  246. bgetc()
  247. {
  248.     if( remchars-- > 0 )
  249.         return( *curbyte++ );
  250.     else
  251.     {
  252.         if( numchars < BIGBUF )
  253.         {
  254.             numchars = BIGBUF;        /* reset for next file */
  255.             return( EOF );
  256.         }
  257.         else
  258.         {
  259.             fillbuf();
  260.             return( bgetc() );
  261.         }
  262.     }
  263. }
  264.  
  265. fillbuf()
  266. {
  267.     static unsigned int i;
  268.  
  269.     if( kickit )
  270.     {
  271.         if( ( begbuf = sbrk( BIGBUF ) ) == -1 )
  272.         {
  273.             printf( "\nError: cannot allocate buffer.\n" );
  274.             printf( "Try smaller buffer size.\n" );
  275.             exit(1);
  276.         }
  277.         kickit = false;
  278.     }
  279.     i = 1;
  280.     curbyte = begbuf;
  281.  
  282. /* read until either EOF or buffer is full */
  283.     while( ( *curbyte++ = getc( filep ) ) != EOF )
  284.         if( i == BIGBUF )
  285.             break;
  286.         else
  287.             i++;
  288.  
  289.     remchars = numchars = i;
  290.     curbyte = begbuf;
  291. }
  292.  
  293. doindent()
  294. {
  295.     int i;
  296.     for( i=0 ; i<indent ; i++ )
  297.         putchar( SPACE );
  298. }
  299. # of available chars remaining in buffer */
  300. static char *begbuf;